<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
use think\Session;
use think\Request;
class Login extends Controller
{
    public function index()
    {

    }
	/*
	 *管理员登录页面
	 */
	public function login(){
		$config = Db::table('config') -> where('id',1) -> find();

		$this->assign('config',$config);

		return $this -> fetch();
	}
	public function doLogin(){
		$data = Request::instance() -> Param();
		if(!captcha_check($data['captcha'])){
					//验证失败
				return(array(
					'error' => 1,
					'msg'   => '验证码错误'
				));
			};
		$name = Db::table('user') -> where('name',$data['name']) -> find();
		if(!$name){
			return(array(
				'error' => 1,
				'msg'   => '账号或密码错误！！！'
			));
		}else{
			if($name['password'] != md5($data['password'])){
				return(array(
					'error' => 1,
					'msg'   => '账号或密码错误！！！'
				));
			}else{
				$time = time();
				Db::table('user') -> where('id',$name['id']) -> update(array('logintime'=>$time));
				Session::set('user',$name);
				return(array(
					'error' => 0,
					'msg'   => '登录成功',
					'url'   => '/admin/'
				));
			}
		}
	}
	/*
	 * 退出登录
	 * */
	public function logout(){
		// 清除session（当前作用域）
		Session::clear();
		$this->redirect('/admin/Login/login',301);
	}
}
